package com.leo.apptest.activity;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;

import com.leo.apptest.R;
import com.leo.apptest.adapter.AnimRecyclerAdapter;

import java.util.ArrayList;
import java.util.List;

import jp.wasabeef.recyclerview.animators.SlideInLeftAnimator;


public class RecyclerAnimActivity extends AppCompatActivity {

    private RecyclerView mRlAnim;
    private AnimRecyclerAdapter mAdapter;
    private Button mBtPress;
    private int mPos;
    private Handler mHandler;
    private List<String> mStringList;
    private MyRunnable mMyRunnable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_anim);
        initViews();
    }

    private void initViews() {
        mMyRunnable = new MyRunnable();
        mStringList = new ArrayList<>();
        mHandler = new Handler();
        mBtPress = (Button) findViewById(R.id.btPress);
        mBtPress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPos++;
                mRlAnim.scrollToPosition(0);
                mAdapter.removeData();
            }
        });
        mRlAnim = (RecyclerView) findViewById(R.id.recyclerAnim);
        mRlAnim.setLayoutManager(new LinearLayoutManager(this));
        mRlAnim.setItemAnimator(new SlideInLeftAnimator());
        mRlAnim.getItemAnimator().setAddDuration(500);
        mAdapter = new AnimRecyclerAdapter();
        mRlAnim.setAdapter(mAdapter);
        mHandler.postDelayed(mMyRunnable, 500);
    }

    private class MyRunnable implements Runnable {

        @Override
        public void run() {
            mPos++;
            if (mPos > 15) {
                return;
            }
            mRlAnim.scrollToPosition(0);
            mAdapter.addFirstData("sdsd" + mPos);
            mHandler.postDelayed(mMyRunnable, 500);
        }
    }


}
